const { ethers } = require("hardhat");

async function main() {
    // 假设你已经部署了ERC20合约，并获得了合约地址和转账的账户地址
    const tokenAddress = "0x962c0940d72E7Db6c9a5F81f1cA87D8DB2B82A23";
    // 发送地址
    const senderAddress = "0xE15aca936a47FeAfc8a80B01f57c9f5e7e966666";
    // 接收地址
    const recipientAddress = "0xe2CCA88aE9cE333cbbfD418771705632B9C88888";
    // const amountToTransfer = ethers.utils.parseUnits("1", "ether"); // 转账金额，这里假设是1个代币
    // const amount = 6000000000000000000; // 大额转账失败
    const amount = ethers.BigNumber.from("666666000000000000");
    // console.log(amount.toString());

    const transferTimes = 3000; // 循环转账的次数

    // 获取合约实例
    const tokenContract = await ethers.getContractAt("MTKcoin", tokenAddress);

    // 获取转账发起者的签名者
    // 获取发送者权限信息方式1
    // const [sender] = await ethers.getSigners();

    // 获取发送者权限信息方式2
    // 提供具体的发送者私钥
    const senderPrivateKey = "69a1b26b9d34f54b75b0c19f148acd9a7017c6044456e7bf0123a84799be0697";
    // 创建一个签名器实例
    const sender = new ethers.Wallet(senderPrivateKey, ethers.provider); 
    // 判断预期发送地址与实际发送地址是否相等
    if (sender.address !== senderAddress) {
        throw new Error("Sender address does not match the signer's address");
    }

    // 循环转账
    for (let i = 0; i < transferTimes; i++) {
        console.log(`正在进行第 ${i + 1} 次转账...`);
        const tx = await tokenContract.connect(sender).transfer(recipientAddress, amount);
        console.log("tx info: ", tx);
        // await tx.wait(); // 等待交易确认
        // console.log(`第 ${i + 1} 次转账成功！`);
    }
}

main()
    .then(() => process.exit(0))
    .catch((error) => {
        console.error(error);
        process.exit(1);
    });
